import request from '@/config/axios'

interface MeterVo {
  id: number
  commId: number
  meterSerialServerId: number
  meterSerialServerName_: string
  meterName: string
  meterNo: string
  productId: string
  productName_: string
  externalLinkId: number
  sort: number
  serialServer: {
    id: number
    serialServerName: string
    factoryCode: string
  }
  price: number
  hxChargeTplId: number
  hxChargeTplName: string
  matCodeId: number
  matCodeUserName_: string
  ownMeterConfigRespVO: {
    memberUserName: any
  }
}

interface MeterParamVo {
  //电表ID
  meterId: number
  //设备编号
  code: string
  //电价类型是否为复费率
  electricPriceType: boolean
  //保电状态 0：保电，1：不保电
  electrictStatus: string
  //透支金额（元）
  overdraftAmount: number
  // 负荷限制功率（kW） -（单相 22.0默认）
  loadLimitPower: number
  // 超负荷延时间 - （单相 10默认）
  loadDelayTime: number
  // 电流互感器变比 -（单相 1默认）
  currentTransformerRatio: number
  // 电压互感器变比 - （单相 1默认）
  voltageTransformerRatio: number
  // 能耗计费方案id
  plmChargeTemplateId: number
  // 一级报警金额（元）
  oneLevelAmount: number
  // 二级报警金额（元）
  twoLevelAmount: number
  // 时区时段设置id
  zoneId: number
}

// 创建电表
export const createMeter = async (data: MeterVo) => {
  return await request.post({
    url: '/zwsj/meter/create',
    data: data
  })
}

// 更新电表
export const updateMeter = async (data: MeterVo) => {
  return await request.put({
    url: '/zwsj/meter/update',
    data: data
  })
}

// 删除电表
export const deleteMeter = async (id: number) => {
  return await request.delete({
    url: `/zwsj/meter/delete?id=` + id
  })
}

// 获得电表
export const getMeter = async (id: number) => {
  return await request.get({
    url: `/zwsj/meter/get?id=` + id
  })
}

// 获取房间下的电表
export const getRoomRelaMeter = async (roomId: number) => {
  return await request.get({
    url: `/zwsj/meter/get/room/${roomId}/meter`
  })
}

// 获得电表分页
export const getMeterPage = async (params: any) => {
  return await request.get({
    url: '/zwsj/meter/page',
    params: params
  })
}

// 外系统接口-电表抄表并更新至本系统中
export const updateMeterParamByCallReadOut = async (id: number) => {
  return await request.get({
    url: `/zwsj/meter/readout?id=` + id
  })
}

// 外系统接口-保电
export const openProtect = async (id: number) => {
  return await request.get({
    url: `/zwsj/meter/open/protect?id=` + id
  })
}

// 外系统接口-取消保电
export const closeProtect = async (id: number) => {
  return await request.get({
    url: `/zwsj/meter/close/protect?id=` + id
  })
}

//
export const updateMeterModeFlag = async (id: number, modeFlag: number) => {
  return await request.put({
    url: `/zwsj/meter/mode/flag?id=${id}&modeFlag=${modeFlag}`
  })
}

// 外系统接口-电表合闸（通电）
export const updateMeterToHz = async (id: number) => {
  return await request.get({
    url: '/zwsj/meter/hz?id=' + id,
    method: 'get'
  })
}

// 外系统接口-电表断电（断电）
export const updateMeterToLz = async (id: number) => {
  return await request.get({
    url: `/zwsj/meter/lz?id=` + id
  })
}

// 外系统接口-电表余额清零
export const clearMeterBalance = async (id: number) => {
  return await request.post({
    url: `/zwsj/meter/balance/clear?id=` + id
  })
}

// 外系统接口-宏新能源-电价方案列表
export const getHxnyPlmChargeTplPage = async (params: any) => {
  return await request.get({
    url: `/zwsj/meter/hxny/plm/charge/tpls`,
    params: params  
  })
}

// 外系统接口-读取电价
export const getMeterUnitPrice = async (id: number) => {
  return await request.get({
    url: `/zwsj/meter/unit/price?id=` + id
  })
}

// 外系统接口-设置电价
export const updateMeterUnitPrice = async (data: any) => {
  return await request.post({
    url: `/zwsj/meter/unit/price`,
    data: data
  })
}

// 外系统接口-读取透支金额
export const getMeterOverDraft = async (id: number) => {
  return await request.get({
    url: `/zwsj/meter/over/draft?id=${id}`
  })
}

// 外系统接口-设置透支金额
export const updateMeterOverDraft = async (id: number, overDraftMoney: string) => {
  return await request.post({
    url: `/zwsj/meter/over/draft?id=${id}&overDraftMoney=${overDraftMoney}`
  })
}

// 外系统接口-跑电查询
export const getMeterUsePowers = async (id: number, dateScope: string) => {
  return await request.get({
    url: `/zwsj/meter/use/powers?id=${id}&dateScope=${dateScope}`
  })
}

// 外系统接口-充值
export const recharge = async (id: number, rechargeMoney: string) => {
  return await request.post({
    url: `/zwsj/meter/recharge?id=${id}&rechargeMoney=${rechargeMoney}`
  })
}

// 外系统接口-充值查询
export const geMeterRecharges = async (params: any) => {
  return await request.get({
    url: `/zwsj/meter/recharge/query`,
    params: params
  })
}


// 下载用户导入模板
export const importMeterTemplate = () => {
  return request.download({ url: '/zwsj/meter/get-import-template' })
}


//弘新能源-获取下发的参数（本地表数据查询）
export const getHxnyMeterDownParam = async (meterId: number) => {
  return await request.get({
    url: `/zwsj/meter/hxny/meter/${meterId}/down/param`
  })
}


//弘新能源-参数下发（本地表数据存储并下发到接口端）
export const updateHxnyMeterDownParam = async (data: any) => {
  return await request.post({
    url: `/zwsj/meter/hxny/meter/down/param`,
    data: data
  })
}

